﻿window.fbAsyncInit = function () {
    FB.init({
        appId: '150800278297282',
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });
    ShowHideFBControls();
    //subscribing to session change event
    FB.Event.subscribe('auth.sessionChange', function (response) {
        // do something with response.session
    });
    //subscribing to login event
    FB.Event.subscribe('auth.login', function (response) {
        ShowHideFBControls();
    });
    //subscribing to logout event
    FB.Event.subscribe('auth.logout', function (response) {
        ShowHideFBControls();
    });
};

function LoadFbJsSdk() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}

function ShowHideFBControls() {
    if (FB != null) {
        FB.getLoginStatus(function (response) {
            if (response.status == "connected") {
                $('#fbLoginDiv').hide();
                $('#fbLogonDiv').show();
                FB.api('/me', function (user) {
                    if (user != null) {
                        var image = document.getElementById('fbLogonDivProfilePic');
                        image.src = 'http://graph.facebook.com/' + user.id + '/picture';
                        var name = document.getElementById('fbLogonDivUserName');
                        name.innerHTML = user.name
                    }
                });


            }
            else {
                $('#fbLoginDiv').show();
                $('#fbLogonDiv').hide();
            }

            if (response.session) {
                //alert("Logedin");
            } else {
                //alert("not loged in");
            }
        });
    }
}

function CallFBLogout() {
    FB.logout(function (response) {
        ShowHideFBControls();
    });

}

function CallFBLoginPopup() {
    if (FB != null) {
        FB.login(function (response) {
            if (response.session) {
                if (response.perms) {
                    // user is logged in and granted some permissions.
                    // perms is a comma separated list of granted permissions
                } else {
                    // user is logged in, but did not grant any permissions
                }
                //window.location.href = "/pages/FB/FBLogon.ashx?returnUrl=" + window.location.href;
                $.get("/pages/FB/Facebook.ashx", { pblogon: "1" },
                            function (data) {
                                        //todo
                            } );
            } else {
                // user is not logged in
            }
        });

    }
}

function FBCallMeApi() {
    FB.getLoginStatus(function (response) {
        if (response.status == "connected") {

            FB.api('/me', function (user) {
                if (user != null) {
                    var image = document.getElementById('fbLogonDivProfilePic');
                    image.src = 'http://graph.facebook.com/' + user.id + '/picture';
                    var name = document.getElementById('fbLogonDivUserName');
                    name.innerHTML = user.name
                }
            });

        }
        else {

        }


    });

}